/***************************************************************************************************
This program computes consolidated holdings and transactions asset class per asset class

It requires a dataset with the following variable: 
stocks_nonreg housing nt_housing housing_pb nt_housing_pb stocks nt_stocks stocks_pb nt_stocks_pb mfund nt_mfund equity_pb nt_equity_pb deposits deposits_pb debt debt_pb net_private_debt bonds
kappa_pb_deposits kappa_pb_debt kappa_pb_housing kappa_pb_stocks kappa_pb_nt_housing kappa_pb_nt_stocks equityQ (available from the dataset Save/kappa.dta)

To construct consolidated holdings/transactions, the program uses the following methodology
1. We start with holdings/transactions directly done by individual
2. We then add holdings/transactions through private businesses. Before 2005, we do not observe them at the individual level, and so we distribute the ones observed in the national accounts proportionally to ownership in private businesses (stocks_nonreg)
3. We then add holdings/transactions through public businesses. We do not try to do so at individual level, and so we distribute the ones observed in the national accounts proportionally to ownership in public businesses (stocks, mfund, stocks_pb)
***************************************************************************************************/

program define gen_transactions
	/* before 2005: impute indirect holdings and transactions through business ownership using need to do imputation for transactions done by private businesses before 2005 */	
	replace deposits_pb = kappa_pb_deposits * stocks_nonreg if year < 2005
	replace debt_pb = kappa_pb_debt * stocks_nonreg if year < 2005
	replace housing_pb = kappa_pb_housing * stocks_nonreg if year < 2005
	replace stocks_pb = kappa_pb_stocks * stocks_nonreg if year < 2005
	replace nt_housing_pb = kappa_pb_nt_housing * stocks_nonreg if year < 2005
	replace nt_stocks_pb = kappa_pb_nt_stocks * stocks_nonreg if year < 2005

	/* compute consolidated holdings and transactions */
	gen housing_total = housing + housing_pb
	gen nt_housing_total = nt_housing + nt_housing_pb + kappa_nt_housing * (stocks + mfund + stocks_pb)
	gen deposits_total = deposits + deposits_pb + kappa_deposits * (stocks + mfund + stocks_pb)
	* Classification of bonds and net_private_debt here is to mirror the concept of debt used in the FA accounts
	gen debt_total = debt - net_private_debt - bonds + debt_pb + kappa_debt * (stocks + mfund + stocks_pb)
	gen equity_total = stocks + mfund + stocks_pb + equity_pb * equityQ
	gen nt_equity_total = nt_stocks + nt_mfund + nt_stocks_pb + nt_equity_pb * equityQ + kappa_nt_stocks * (stocks + mfund + stocks_pb)
end





